<? include 'server_config.php'; 

if($_SESSION['user_idx'] == "")
{
	?>
	<script>
		location.href='login.php';
	</script>
	<?
	return;
}

$info_idx = $user_idx;
?>
			<script type="text/javascript" src="js/jquery-1.6.2.min.js"></script>
		<div style="width:800px; float:left;">
			<div style="width:800px; height:26px; vertical-align : middle; display : table-cell;">
				류마티스 점수 그래프
			</div>
			<div style="width:800px; height:150px; background:#eeeeee;">
<?

			$sql="
				SELECT distinct(reg_date) FROM ndd_ra_score_input WHERE user_idx = $info_idx ORDER BY reg_date;
			";
			
			//echo $sql;

			$result = mysql_query($sql,$connect);

			while($row = mysql_fetch_row($result))
			{
				echo $row[0]."<br>";
			
			
			}

?>			

			</div>			

			
<?
			//page_editable

			$sql="
				INSERT INTO ndd_user_expand (user_idx) VALUES ('$info_idx');
			";
			
			$result = mysql_query($sql,$connect);

			$sql="
				SELECT A.nickname, B.birthday, B.sex FROM ndd_user A, ndd_user_expand B WHERE A.user_idx = '$info_idx' AND A.user_idx = B.user_idx LIMIT 1;
			";
			
//			echo $sql;

			$result = mysql_query($sql,$connect);

			$row = mysql_fetch_row($result);

			$info_nickname = $row[0];
			$info_birthdaty = $row[1];
			$info_sex = $row[2];



			/*$sql="
				SELECT * FROM ndd_ra_score_input WHERE user_idx = '$info_idx' AND reg_date = CURRENT_DATE LIMIT 1;
			";
*/
			$sql="
				SELECT * FROM ndd_ra_score_input WHERE user_idx = '$info_idx' ORDER BY reg_date DESC LIMIT 1;
			";
			
//			echo $sql;

			$result = mysql_query($sql,$connect);

			$row = mysql_fetch_row($result);




			$percent = 0; $score = 0;
?>			

			환자 : <? echo $info_nickname; ?> <br>
			성별 : <? 
					if($info_sex=="M")
						echo "남자";
					else if($info_sex=="F")
						echo "여자";
					else 
						echo "(미입력)";
					
			?> / 나이 : 
			<?	
					echo $row[3]."세";

			?>
			
			/ 분석날짜 : <? echo $row[2]; ?><br>
			<br>
			<b>1. 영역별 결과</b><br>
			<br>
			관절 : <span id='big_count'>0</span>개의 큰 관절과 <span id='small_count'>0</span>개의 작은 관절<br>
			<br>
			류마티스 수치 : 
			
			<? if($row[13]==1)
			{
				echo "RF / ".$row[14];

				$rf_uln = 30;

				if($row[14] > 3*$rf_uln)
				{
					$percent += 33.9; $score += 3;
				}
				else if($row[14] > $rf_uln)
				{
					$percent += 22; $score += 2;
				}

			}
			else if($row[15]==1)
			{
				echo "ACPA / ".$row[16];

				$acpa_uln = 30;

				if($row[16] > 3*$acpa_uln)
				{
					$percent += 33.9; $score += 3;
				}
				else if($row[16] > $acpa_uln)
				{
					$percent += 22; $score += 2;
				}
			}
			else {
				echo "(미입력)";
			}
			?><br>
			<br>
			급성염증 수치 : 
			<? if($row[17]==1)
			{
				echo "CRP / ".$row[18];

				$crp_uln = 3.0;

				if($row[18] > $crp_uln)
				{
					$percent += 5.9; $score += 1;
				}

			}
			else if($row[19]==1)
			{
				echo "ESR / ".$row[20];

				$esr_uln = 20.0;
				if($info_sex=="M")
					$esr_uln = 15.0;


				if($row[20] > $esr_uln)
				{
					$percent += 5.9; $score += 1;
				}
			}
			else {
				echo "(미입력)";
			}
			?><br>
			<br>
			얼마나 오랫동안 : 
			<?

			if($row[5]==1)
			{
				echo "6주 이상";
				$percent += 9.3; $score += 1;
			}
			else
			{
				echo "6주 이하";
			}
			?><br>
			<br>
			<br>
			<b>2. 종합 평가</b><br>

<?
				$big_count=0;

				for($i=1;$i<=10;$i++)
				{
					
					if($row[4][$i-1]=="1") 
						$big_count++;
				}

				$small_count=0;

				for($i=11;$i<=50;$i++)
				{
					
					if($row[4][$i-1]=="1") 
						$small_count++;
				}

				$other_count=0;

				for($i=51;$i<=53;$i++)
				{
					
					if($row[4][$i-1]=="1") 
						$other_count++;
				}

				$both_side = false;

				for($i=1;$i<=5;$i++)
				{
					
					if($row[4][$i-1]=="1" && $row[4][$i-1+5]=="1") 
						$both_side = true;
				}





				$x=$big_count;
				$y=$small_count;
				$z=$other_count;

				if( $x+$y+$z > 10 && $y > 0 )
				{
					$percent += 50.8; $score += 5;
				}
				else if( 10 >= $y && $y >= 4)
				{
					$percent += 28.8; $score += 3;
				}
				else if( 3 >= $y && $y >= 1)
				{
					$percent += 21.2; $score += 2;
				}
				else if( 10 >= $x && $x >= 2)
				{
					if ($both_side)
					{
						$percent += 16.1; $score += 1;
					}
					else
					{
						$percent += 10.2; $score += 1;
					}
				}



						
?>
			<br>
			총 류마티스 확률 : <? echo $percent; ?>퍼센트  <? echo $score; ?>/10점<br>
			<br>
			<br>
			<script>
				
				
				function value_init()
				{
					
					var big_count = <?

						echo $big_count;
						
					?>;
					var small_count = <?

						echo $small_count;
						
					?>;




					$('#small_count').html(small_count);
					$('#big_count').html(big_count);



					
					
				
				}
				
				value_init();
			</script>
			<br>
		</div>


<?

			$sql="
				UPDATE ndd_ra_score_input SET score = '$score' WHERE  user_idx = '$info_idx' AND reg_date = CURRENT_DATE LIMIT 1;
			";
			
			echo $sql;

			$result = mysql_query($sql,$connect);






 ?>
			
			<script>
				location.replace("5_1.php?view=ra");
			</script>